﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Collections;
using V5_EaspLibs;

namespace V5_FrameWork.Data {
    /// <summary>
    /// 地址生成帮助
    /// </summary>
    public class BaseUrlHelper {
        /// <summary>
        /// 模板地址转换
        /// </summary>
        /// <param name="Controller"></param>
        /// <returns></returns>
        public string SplitController(string Controller) {
            if (Controller.IndexOf("-") > -1) {
                if (Controller.IndexOf("-") > -1) {
                    Controller = Controller.Replace("-", "_");
                }
            }
            return Controller;
        }
        /// <summary>
        /// 生成文件地址
        /// </summary>
        /// <param name="Controller">模板文件</param>
        /// <param name="attributes">属性new{ID="1",PageID="1"}</param>
        /// <param name="ShowType">显示类型 1.为普通2.为伪静态3.静态</param>
        /// <returns></returns>
        public string Action(string Controller, object attributes, int ShowType) {
            StringBuilder sResult = new StringBuilder();
            sResult = new StringBuilder();
            if (ShowType == 1) {
                sResult.Append("index.aspx?t=" + Controller);
                if (attributes != null) {
                    PropertyInfo[] pp = attributes.GetType().GetProperties();
                    foreach (PropertyInfo p in pp) {
                        string value = p.GetValue(attributes, null).ToString();
                        sResult.Append("&" + p.Name + "=" + value);
                        //sResult.Append("-" + value);
                    }
                    //sResult.Append(".html");
                }
            }
            else if (ShowType == 2) {
                sResult.Append(Controller);
                if (attributes != null) {
                    PropertyInfo[] pp = attributes.GetType().GetProperties();
                    foreach (PropertyInfo p in pp) {
                        string value = p.GetValue(attributes, null).ToString();
                        sResult.Append("-" + value);
                    }
                }
                sResult.Append(".aspx");
            }
            else if (ShowType == 3) {
                sResult.Append("html/" + Controller + "/");
                if (attributes != null) {
                    PropertyInfo[] pp = attributes.GetType().GetProperties();
                    foreach (PropertyInfo p in pp) {
                        string value = p.GetValue(attributes, null).ToString();
                        sResult.Append(value + "-");
                    }
                }
                if (sResult.Length > 0) {
                    sResult = sResult.Remove(sResult.Length - 1, 1);
                }
                sResult.Append(".html");
            }
            return "/" + sResult.ToString().ToLower();
        }
        /// <summary>
        /// 生成文件地址
        /// </summary>
        /// <param name="Controller"></param>
        /// <param name="attributes"></param>
        /// <param name="ShowType"></param>
        /// <returns></returns>
        public string Action(string Controller, IDictionary attributes, int ShowType) {
            StringBuilder sResult = new StringBuilder();
            sResult = new StringBuilder();
            if (ShowType == 1) {
                sResult.Append("index.aspx?t=" + Controller);
                foreach (object key in attributes.Keys) {
                    object value = attributes[key];
                    sResult.Append("&" + key + "=" + value);
                }
            }
            else if (ShowType == 2) {
                sResult.Append(Controller);
                foreach (object key in attributes.Keys) {
                    object value = attributes[key];
                    sResult.Append("-" + value);
                }
                sResult.Append(".aspx");
            }
            else if (ShowType == 3) {
                Controller = Controller.Replace("-", "/");
                Controller = "/" + Controller + "/";
                sResult.Append(Controller);
                foreach (object key in attributes.Keys) {
                    object value = attributes[key];
                    sResult.Append(value + "-");
                }
                if (sResult.Length > 0) {
                    sResult = sResult.Remove(sResult.Length - 1, 1);
                }
                sResult.Append(".html");
            }
            return "/" + sResult.ToString();
        }


        //public string UrlView(string lookFor, string ID, string PageID, string S, string Title, string AddDateTime, int ShowType, bool isList, bool isSearch, string RewriteFileName, string HtmlFileName) {
        //    string Url = string.Empty;
        //    if (ShowType == 1) {
        //        if (isList) {
        //            Url = "index.aspx?t=" + lookFor + "&ClassID=" + ID + "&PageID=" + PageID;
        //        }
        //        else if (isSearch) {
        //            Url = "index.aspx?t=" + lookFor + "&Search=" + S + "&PageID=" + PageID;
        //        }
        //        else {
        //            Url = "index.aspx?t=" + lookFor + "&ID=" + ID + "&PageID=" + PageID;
        //        }
        //    }
        //    if (ShowType == 2) {
        //        if (RewriteFileName == "1") {
        //            if (isSearch) {
        //                Url = lookFor + "-" + S + "-" + PageID + ".aspx";
        //            }
        //            else {
        //                Url = lookFor + "-" + ID + "-" + PageID + ".aspx";
        //            }
        //        }
        //        else {
        //            if (isSearch) {
        //                Url = lookFor + "-" + S + "-" + PageID + "/";
        //            }
        //            else {
        //                Url = lookFor + "-" + ID + "-" + PageID + "/";
        //            }
        //        }
        //    }
        //    if (ShowType == 3) {
        //        Title = Easp.Pinyin.Convert(Title);
        //        string TimeStr = !string.IsNullOrEmpty(AddDateTime) ? Convert.ToDateTime(AddDateTime).ToString("yyyy/MM") : string.Empty;
        //        if (HtmlFileName == "1") {
        //            Url = SplitController(lookFor) + "/" + ID + "-" + PageID + ".html";
        //        }
        //        else if (HtmlFileName == "2") {
        //            Url = SplitController(lookFor) + "/" + Title + "-" + PageID + ".html";
        //        }
        //        else if (HtmlFileName == "3") {
        //            if (!string.IsNullOrEmpty(TimeStr)) {
        //                Url = SplitController(lookFor) + "/" + TimeStr + "/" + ID + "-" + PageID + ".html";
        //            }
        //            else {
        //                Url = SplitController(lookFor) + "/" + ID + "-" + PageID + ".html";
        //            }
        //        }
        //        else if (HtmlFileName == "4") {
        //            if (!string.IsNullOrEmpty(TimeStr)) {
        //                Url = SplitController(lookFor) + "/" + TimeStr + "/" + Title + "-" + PageID + ".html";
        //            }
        //            else {
        //                Url = SplitController(lookFor) + "/" + Title + "-" + PageID + ".html";
        //            }
        //        }
        //    }
        //    return Url;
        //}
    }
}
